<template>
  <div class="car-review-container">
    <!-- 汽车图片和标题展示区 -->
    <div class="car-header-section">
      <div class="image-container">
        <img :src="currentCar.picUrl || '/src/images/demo1.jpg'" alt="汽车展示图" class="main-image">
      </div>
      <div class="title-container">
        <h2>{{ currentCar.modelName || '示例车型' }}</h2>
        <p>{{ currentCar.modelVersion || '未知版本' }}</p>
      </div>
    </div>

    <!-- 六维评分区 -->
    <div class="rating-section">
      <h3>请为车辆评分</h3>
      <div class="rating-grid">
        <div class="rating-item" v-for="(item, index) in ratingItems" :key="index">
          <label>{{ item.label }}</label>
          <div class="star-rating">
            <span 
              v-for="star in 5" 
              :key="star"
              @click="setRating(index, star)"
              :class="{ 'active': star <= item.value }">
              ★
            </span>
          </div>
        </div>
      </div>
    </div>

    <!-- 评论输入区 -->
    <div class="comment-section">
      <textarea 
        v-model="commentText" 
        placeholder="请输入您的评论..."
        class="comment-input"></textarea>
      
      <!-- <div class="image-upload">
        <input 
          type="file" 
          @change="handleImageUpload" 
          multiple
          accept="image/*" />
        <div class="preview-images">
          <img 
            v-for="(img, idx) in previewImages" 
            :key="idx" 
            :src="img" 
            class="preview-image" />
        </div>
      </div> -->
      
      <button @click="submitReview" class="submit-btn">提交评论</button>
    </div>
  </div>
    <!-- 汽车基本信息 -->
    
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import api from '@/api/index.js';

const route = useRoute();
const modelId = ref(route.params.modelId ); // 从路由参数获取modelId，默认1
const userInfoStr = localStorage.getItem('userInfo');
const userInfo = userInfoStr ? JSON.parse(userInfoStr) : {};
const userId = userInfo.userId || '';

const carName = ref('示例车型');
const currentCar = ref({});
const error = ref('');
const ratingItems = ref([
  { label: '动力', value: 0, field: 'powerScore' },
  { label: '内饰', value: 0, field: 'interiorScore' },
  { label: '舒适性', value: 0, field: 'comfortScore' },
  { label: '配置', value: 0, field: 'configScore' },
  { label: '操控', value: 0, field: 'handlingScore' },
  { label: '空间', value: 0, field: 'spaceScore' }
]);

const commentText = ref('');
const previewImages = ref([]);
const loading = ref(false);
const submitSuccess = ref(false);

const setRating = (index, value) => {
  ratingItems.value[index].value = value;
};

// 获取车辆详情数据
const fetchCarDetails = async (modelId) => {
  try {
    console.log('22222...',modelId);
    loading.value = true;
    // 使用searchCarsByDTO方法并传入modelId进行查询
    const response = await api.searchCarsByDTO({ modelId });
    console.log('获取到的车辆详情数据1111:', response.data);

    if (response.data && response.data.flag) {
      // 假设返回的是数组，取第一个元素作为当前车辆
      currentCar.value = response.data.result.length > 0 ? response.data.result[0] : {};
      if (Object.keys(currentCar.value).length === 0) {
        error.value = '未找到该车型数据';
      }
    } else {
      error.value = '获取车辆数据失败';
      console.error('获取车辆数据失败:', response.data?.reason || '未知错误');
    }
  } catch (err) {
    error.value = '网络错误，无法获取车辆数据';
    console.error('获取车辆数据异常:', err);
  } finally {
    loading.value = false;
  }
};

const submitReview = async () => {
  // 正确访问ref的值需要使用.value
  console.log('1111111: modelId =', modelId.value);
  // 验证评分是否都已选择
  if (ratingItems.value.some(item => item.value === 0)) {
    alert('请为所有项目评分');
    return;
  }

  // 验证评论内容是否填写
  if (!commentText.value.trim()) {
    alert('请输入评论内容');
    return;
  }

  loading.value = true;

  try {
    // 构造评论数据
    const commentData = {
      userId: parseInt(userId),
      modelId: parseInt(modelId.value),
      commentDetail: commentText.value,
      // 映射评分数据
      ...ratingItems.value.reduce((acc, item) => {
        acc[item.field] = item.value;
        return acc;
      }, {})
      // 图片数据可以根据后端要求处理，这里暂时省略
    };

    // 调用添加评论接口
    const response = await api.addComment(commentData);

    if (response.data.flag) {
      submitSuccess.value = true;
      alert('评论提交成功！');
      // 重置表单
      commentText.value = '';
      previewImages.value = [];
      ratingItems.value.forEach(item => { item.value = 0; });
    } else {
      alert('评论提交失败：' + response.data.reason);
    }
  } catch (error) {
    console.error('提交评论失败:', error);
    alert('网络异常，请稍后重试');
  } finally {
    loading.value = false;
  }
};

onMounted(() => {

  fetchCarDetails(modelId.value);
});

</script>

<style scoped>
.car-review-container {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.car-header-section {
  display: flex;
  align-items: center;
  gap: 40px;
  margin-bottom: 30px;
}

.title-container {
  flex: 1;
}

.image-container {
  width: 640px;
  height: 360px;
  overflow: hidden;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

.main-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.main-image:hover {
  transform: scale(1.05);
}

.rating-section {
  margin-bottom: 30px;
}

.rating-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
}

.rating-item {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.rating-item label {
  width: 80px;
  margin-right: 15px;
}

.star-rating span {
  font-size: 24px;
  color: #ccc;
  cursor: pointer;
  margin-right: 5px;
}

.star-rating span.active {
  color: #ffc107;
}

.comment-section {
  margin-top: 30px;
}

.comment-input {
  width: 100%;
  height: 120px;
  padding: 10px;
  margin-bottom: 15px;
  border: 1px solid #ddd;
  border-radius: 4px;
}

.image-upload {
  margin-bottom: 20px;
}

.preview-images {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
}

.preview-image {
  width: 120px;
  height: 90px;
  object-fit: cover;
  border-radius: 4px;
  margin: 5px;
}

.submit-btn {
  background-color: #4CAF50;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.submit-btn:hover {
  background-color: #45a049;
}
.review-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 2rem;
  font-family: Arial, sans-serif;
  color: #333;
}

/* 汽车头部信息 */
.car-header {
  display: flex;
  align-items: center;
  gap: 2rem;
  margin-bottom: 2rem;
  padding-bottom: 2rem;
  border-bottom: 1px solid #eee;
}

.car-image-container {
  width: 300px;
  height: 200px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.main-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.car-info {
  flex: 1;
}

.car-title {
  font-size: 2rem;
  margin-bottom: 0.5rem;
}

.car-type {
  color: #666;
  font-size: 1.2rem;
}

/* 评分区域 */
.rating-section {
  margin-bottom: 3rem;
  padding-bottom: 2rem;
  border-bottom: 1px solid #eee;
}

.section-title {
  font-size: 1.5rem;
  margin-bottom: 1.5rem;
  color: #222;
}

.rating-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 1.5rem;
}

.rating-item {
  display: flex;
  align-items: center;
  gap: 1rem;
}

.rating-label {
  width: 80px;
  font-weight: bold;
}

.rating-stars {
  flex: 1;
}

.star {
  color: #ccc;
  font-size: 1.2rem;
  margin-right: 0.3rem;
}

.star.active {
  color: #ffc107;
}

.rating-value {
  width: 60px;
  text-align: right;
  color: #666;
}

/* 评论区域 */
.comment-list {
  margin-bottom: 2rem;
}

.comment-item {
  padding: 1.5rem 0;
  border-bottom: 1px solid #eee;
}

.comment-header {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.8rem;
}

.user-info {
  display: flex;
  flex-direction: column;
}

.user-name {
  font-weight: bold;
  margin-bottom: 0.3rem;
}

.comment-date {
  color: #999;
  font-size: 0.8rem;
}

.comment-content {
  line-height: 1.6;
  margin-bottom: 1rem;
}

.comment-images {
  display: flex;
  gap: 1rem;
  flex-wrap: wrap;
}

.comment-image {
  width: 120px;
  height: 90px;
  object-fit: cover;
  border-radius: 4px;
  cursor: pointer;
  transition: transform 0.3s;
}

.comment-image:hover {
  transform: scale(1.05);
}

/* 评论输入框 */
.comment-input-container {
  background-color: #f9f9f9;
  padding: 1.5rem;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.comment-textarea {
  width: 100%;
  min-height: 100px;
  padding: 1rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  resize: vertical;
  margin-bottom: 1rem;
  font-family: inherit;
  font-size: 1rem;
}

.input-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.upload-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 1rem;
  background-color: #fff;
  border: 1px solid #ddd;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.upload-btn:hover {
  background-color: #f0f0f0;
}

.preview-images {
  display: flex;
  gap: 1rem;
  margin-top: 1rem;
  flex-wrap: wrap;
}

.preview-item {
  position: relative;
}

.preview-image {
  width: 80px;
  height: 60px;
  object-fit: cover;
  border-radius: 4px;
}

.remove-btn {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 20px;
  height: 20px;
  background-color: #ff4d4f;
  color: white;
  border: none;
  border-radius: 50%;
  cursor: pointer;
  font-size: 0.8rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.submit-btn {
  padding: 0.8rem 1.5rem;
  background-color: #0066cc;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.3s;
}

.submit-btn:hover {
  background-color: #0052a3;
}

@media (max-width: 768px) {
  .car-header {
    flex-direction: column;
    align-items: flex-start;
  }
  
  .car-image-container {
    width: 100%;
    height: auto;
  }
  
  .rating-grid {
    grid-template-columns: 1fr;
  }
}
</style>
